const api = require('../../api/api')
const app = getApp()
const util = require('../../utils/js/util')
Page({
  data: {
    id: '',
    type: '',
    safeBottom: util.getSafeToBottom(),
    popshow: false,
    patient: [],
    page: 1,
    listStatus: 'loading',
    orderInfo: {},
    agreeTap: true,
  },

  async onLoad(options) {
    if (options?.type && options?.id) {
      this.setData({ id: options.id, type: options.type })
    }
    this.getOrder()
    await this.getPatient()
    wx.createIntersectionObserver().relativeTo('#list-container', { bottom: 10 }).observe('#bottomline', (res) => {
      if (res.intersectionRatio > 0 && this.data.listStatus == 'loading' && this.data.patient.length >= 10) {
        this.setData({ page: this.data.page + 1 })
        this.getPatient()
      }
    })
  },

  // 获取订单详情
  async getOrder() {
    let res = null
    const { type, id } = this.data
    if (type == 'service') {
      res = await api.getOrder({ id })
      res.type = 'service'
    } else if (type == 'check') {
      res = await api.getOrderCheck({ id })
      res.check_content = JSON.parse(res.check_content)
      res.type = 'check'
    }
    if(res.client) {
      res.client.age = new Date().getFullYear() - new Date(res.client.birthday).getFullYear()
    }
    this.setData({ orderInfo: res })
  },

  // 更换就诊人弹窗弹出
  replacePatient() {
    this.setData({ popshow: true })
  },

  // 选择就诊人
  async choosePerson({ currentTarget: { dataset: { item } } }) {
    if(this.data.agreeTap) {
      this.setData({ agreeTap: false })
      if (this.data.type == 'service') {
        const res = await api.updateOrder({
          id: this.data.id,
          client_id: item.id
        })
      } else if (this.data.type == 'check') {
        const res = await api.checkUpdateOrder({
          id: this.data.id,
          client_id: item.id
        })
      }
      this.getOrder()
      this.setData({ popshow: false, agreeTap: true })
      util.toast('换绑成功');
    }
  },

  // 添加就诊人
  gotoPatient() {
    wx.navigateTo({ url: '../patient/index' })
  },

  // 获取就诊人列表
  async getPatient() {
    const res = await api.getClientList({ p: this.data.page })
    const newdata = res.data.map(v => ({
      ...v,
      age: new Date().getFullYear() - new Date(v.birthday).getFullYear(),
    }))
    this.setData({
      patient: this.data.page == 1 ? newdata : this.data.patient.concat(newdata),
      listStatus: res.current_page == res.last_page ? 'tobottom' : res.current_page < res.last_page ? 'loading' : res.current_page > res.last_page ? 'nodata' : ''
    })
  },
  
  // 拉起支付
  async pay() {
    const res = await api.pay({ order_type: this.data.orderInfo.type, order_no: this.data.orderInfo.order_no }), pr = res.pay_response
    wx.requestPayment({
      timeStamp: pr.timeStamp,
      nonceStr: pr.nonceStr,
      package: pr.package,
      signType: pr.signType,
      paySign: pr.paySign,
      success: res1 => {
        if (res1.errMsg === "requestPayment:ok") {
          util.toast('支付成功')
          this.getOrder()
        } else if (res1.errMsg === "requestPayment:fail cancel") {
          util.toast('取消支付')
        }
      },
      fail(res1) {
        console.log(res1);
        util.toast('支付失败');
      }
    })
  }
})
